package com.junson.client.codec;

import java.nio.charset.Charset;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * @author: junson
 * @create: 2018-11-28 09:10 
 * @description: Encoder
 */
public class Encoder extends MessageToByteEncoder<ByteBuf> {//EchoClientHandler 请求的数据是byte类型MessageToByteEncoder的泛型是ByteBuf

	private static Logger logger = LoggerFactory.getLogger(Encoder.class);
	private Charset charset;
	
	public Encoder() {
		
	}
	public Encoder(Charset charset) {
		this.charset = charset;
	}
	@Override
	protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception {
		logger.debug("client encoder====>" + msg.toString());
		out.writeInt(msg.readableBytes());
		out.writeBytes(msg);
		
	}
	
	
}
